package com.example.demo.outlook.interceptor;

import com.example.demo.outlook.utils.JwtUtil;
import com.example.demo.outlook.utils.UserContext;
import io.jsonwebtoken.Jwt;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.servlet.HandlerInterceptor;


/**
 * @author linls
 */
@RequiredArgsConstructor
public class LoginInterceptor implements HandlerInterceptor {


    @Resource
    private final JwtUtil jwtUtil;



    /*
    @Override
    public boolean preHandler(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getHeader("authorization");

        Long userId = JwtUtil.parseJwt(token);


        UserContext.setUser(userId);

        return true;
    }


     */


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("authorization");

        Long userId = JwtUtil.parseJwt(token);


        UserContext.setUser(userId);

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserContext.removeUser();
    }
}
